package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.system.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Slf4j
public class OrderStatisticsTask {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;
    //创建定时任务，统计前一天的订单
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics(){
        //DateUtil.offsetDay日期操作方法，-1表示获取前一天的日期，转换成字符串的年月日格式
        String createTime = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("YYYY-MM-dd"));
        //根据前一天的日期查询orderinfo表，
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        //如果有数据，则插入到orderstatistics表中
        if (orderStatistics != null){
            orderStatisticsMapper.insert(orderStatistics);
        }
    }
}
